﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserInterfaceLayer.User
{
    public partial class Safekeeping : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblFailure.Visible = false;
            lblInvalidInput.Visible = false;
            lblSuccessful.Visible = false;
            lblNewItem.Visible = false;
            lblDeletedItem.Visible = false;
            btnDelete.Visible = false;
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            string strSafekeepingID = Request.QueryString["SafekeepingID"];
            if (strSafekeepingID != "add")
            {
                int SafekeepingID;
                if (!int.TryParse(strSafekeepingID, out SafekeepingID))
                    Response.Redirect("~/");
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                DataAccessLayer.Safekeeping Safekeeping = DataAccessLayer.Safekeeping.FindBySafekeepingID_IncludeDeleted(db, SafekeepingID);
                if (Safekeeping == null)
                    Response.Redirect("~/");
                FromDatabase(Safekeeping);
                btnDelete.Visible = true;
                lvHistory.DataSource = BusinessLogicLayer.DataObject.Safekeeping.Convert(DataAccessLayer.Safekeeping.HistoryToListBySafekeepingID(db, SafekeepingID));
                lvHistory.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strSafekeepingID = Request.QueryString["SafekeepingID"];
                string strRequestID = Request.QueryString["RequestID"];
                if (strSafekeepingID != "add")
                {
                    int SafekeepingID = int.Parse(strSafekeepingID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    //DataAccessLayer.Safekeeping Safekeeping = DataAccessLayer.Safekeeping.FindBySafekeepingID_IncludeDeleted(db, SafekeepingID);
                    DataAccessLayer.Safekeeping NewSafekeeping = new DataAccessLayer.Safekeeping();
                    NewSafekeeping.SafekeepingID = SafekeepingID;
                    NewSafekeeping.EditorUserID = UserID;
                    NewSafekeeping.Timestamp = DateTime.Now;
                    ToDatabase(NewSafekeeping);
                    db.Safekeepings.InsertOnSubmit(NewSafekeeping);
                    db.SubmitChanges();
                    lblSuccessful.Visible = true;
                }
                else
                {
                    int RequestID = int.Parse(strRequestID);
                    DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                    DataAccessLayer.sSafekeeping sSafekeeping = new DataAccessLayer.sSafekeeping();
                    sSafekeeping.RequestID = RequestID;
                    db.sSafekeepings.InsertOnSubmit(sSafekeeping);
                    db.SubmitChanges();
                    DataAccessLayer.Safekeeping NewSafekeeping = new DataAccessLayer.Safekeeping();
                    NewSafekeeping.SafekeepingID = sSafekeeping.ID;
                    NewSafekeeping.EditorUserID = UserID;
                    NewSafekeeping.Timestamp = DateTime.Now;
                    ToDatabase(NewSafekeeping);
                    db.Safekeepings.InsertOnSubmit(NewSafekeeping);
                    db.SubmitChanges();
                    lblNewItem.Visible = true;
                    btnSave.Visible = false;
                }
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int UserID = (int)Session["UserID"];
                string strSafekeepingID = Request.QueryString["SafekeepingID"];
                int SafekeepingID = int.Parse(strSafekeepingID);
                DataAccessLayer.linqDatabaseDataContext db = new DataAccessLayer.linqDatabaseDataContext();
                //DataAccessLayer.Safekeeping Safekeeping = DataAccessLayer.Safekeeping.FindBySafekeepingID_IncludeDeleted(db, SafekeepingID);
                DataAccessLayer.Safekeeping NewSafekeeping = new DataAccessLayer.Safekeeping();
                NewSafekeeping.SafekeepingID = SafekeepingID;
                NewSafekeeping.EditorUserID = UserID;
                NewSafekeeping.Timestamp = DateTime.Now;
                NewSafekeeping.IsDeleted = true;
                ToDatabase(NewSafekeeping);
                db.Safekeepings.InsertOnSubmit(NewSafekeeping);
                db.SubmitChanges();
                lblSuccessful.Visible = true;
            }
            catch (Exception)
            {
                lblFailure.Visible = true;
            }
        }

        public void FromDatabase(DataAccessLayer.Safekeeping Safekeeping)
        {
            txtGiveDescription.Text = Safekeeping.GiveDescription;
            txtTakeDescription.Text = Safekeeping.TakeDescription;
            txtTitle.Text = Safekeeping.Title;
            ddlStatus.SelectedValue = Safekeeping.SafekeepingStatusID.ToString();
            lblDeletedItem.Visible = Safekeeping.IsDeleted;
        }

        public void ToDatabase(DataAccessLayer.Safekeeping Safekeeping)
        {
            Safekeeping.GiveDescription = txtGiveDescription.Text;
            Safekeeping.TakeDescription = txtTakeDescription.Text;
            Safekeeping.Title = txtTitle.Text;
            Safekeeping.SafekeepingStatusID = int.Parse(ddlStatus.SelectedValue);
        }
    }
}